package com.jm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.common.exception.ServiceException;
import com.jm.common.util.entity.SelectCondition;
import com.jm.dao.SetmealMapper;
import com.jm.pojo.Category;
import com.jm.pojo.Setmeal;
import com.jm.pojo.SetmealToy;
import com.jm.pojo.dto.SetmealDto;
import com.jm.service.CategoryService;
import com.jm.service.CommonService;
import com.jm.service.SetmealService;
import com.jm.service.SetmealToyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private CommonService commonService;

    @Override
    @Transactional
    public boolean removeBatchByIds(Collection<?> list) {
        for (Object o : list) {
            String id = String.valueOf(o);
            Setmeal setmeal = super.getById(id);
            boolean removeFileResult = commonService.delFile(setmeal.getImage());
            if (!removeFileResult) throw new ServiceException("删除文件失败");
        }
        boolean removeBatchByIds = super.removeBatchByIds(list);
        if (!removeBatchByIds) throw new ServiceException("批量删除失败");
        return true;
    }

    @Autowired
    private SetmealToyService setmealToyService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public IPage<Setmeal> selectByPageCondition(SelectCondition condition) {
        Long currentPage = condition.getPage();
        Long pageSize = condition.getPageSize();
        if (currentPage == null || pageSize == null)
            throw new ServiceException("分页条件出现错误");
//        构造分页
        IPage<Setmeal> page = new Page<>(currentPage, pageSize);
//        构造查询条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(condition.getName() != null, Setmeal::getName, condition.getName());
//        进行查询
        page(page, qw);
        return page;
    }

    @Override
    public boolean changeStatusByIds(List<String> ids, Integer status) {
        List<Setmeal> collect = new ArrayList<>();
        if (ids != null) {
            collect = ids.stream().map(s -> {
                        Setmeal setmeal = new Setmeal(Long.parseLong(s), status);
                        return setmeal;
                    }
            ).collect(Collectors.toList());
        }
        return updateBatchById(collect);
    }

    @Override
    public List<Setmeal> getByCategoryId(String categoryId) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(true, Setmeal::getCategoryId, categoryId);
        return list(qw);
    }

    @Override
    public boolean saveWithSetmealToy(SetmealDto setmealDto) {
        //        处理基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        boolean save = super.save(setmeal);
        if (!save) throw new ServiceException("保存套餐的基本信息失败");
//        处理套餐内的玩具信息 为其设置套餐id
        List<SetmealToy> setmealToys = setmealDto.getSetmealToys();
        for (SetmealToy item : setmealToys) {
            item.setSetmealId(setmeal.getId());
        }
        boolean b = setmealToyService.saveBatch(setmealToys);
        if (!b) throw new ServiceException("保存套餐的菜品信息失败");
        return true;
    }

    @Override
    public SetmealDto selectWithToy(Long id) {
        //        查询基础数据
        Setmeal setmeal = super.getById(id);
//        查询玩具相关数据
        LambdaQueryWrapper<SetmealToy> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, SetmealToy::getSetmealId, id);
        List<SetmealToy> list = setmealToyService.list(lqw);
//        创建相关Dto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealToys(list);
        return setmealDto;
    }

    @Override
    public boolean updateWithDish(SetmealDto setmealDto) {
        //        修改基础部分
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        boolean result1 = super.updateById(setmealDto);
//        修改(删除)玩具数据
        LambdaQueryWrapper<SetmealToy> lqw = new LambdaQueryWrapper<SetmealToy>();
        Long id = setmealDto.getId();
        lqw.eq(true, SetmealToy::getSetmealId, id);
        boolean remove = setmealToyService.remove(lqw);
//        添加玩具数据
        List<SetmealToy> setmealDishes = setmealDto.getSetmealToys();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        boolean result2 = setmealToyService.saveBatch(setmealDishes);
        if (!result1 || !result2 || !remove) throw new ServiceException("修改套餐数据失败");
        return true;
    }

    @Override
    public SetmealDto getDtoBySetmealId(Long setmealId) {
        Setmeal setmeal = getById(setmealId);
        List<SetmealToy> setmealToyList = setmealToyService.getToysBySetmealId(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealToys(setmealToyList);
        return setmealDto;
    }

    @Override
    public SetmealDto toDto(Setmeal setmeal) {
        SetmealDto dto = new SetmealDto();
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        BeanUtils.copyProperties(setmeal, dto);
        dto.setCategoryName(category.getName());
        return dto;
    }

    @Override
    public IPage<SetmealDto> toDto(IPage<Setmeal> setmealIPage) {
        List<SetmealDto> collect = setmealIPage.getRecords().stream().map(new Function<Setmeal, SetmealDto>() {
            @Override
            public SetmealDto apply(Setmeal setmeal) {
                return toDto(setmeal);
            }
        }).collect(Collectors.toList());
        IPage<SetmealDto> dto = new Page<>();
        BeanUtils.copyProperties(setmealIPage, dto, "records");
        dto.setRecords(collect);
        return dto;
    }
}
